<?php
/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2016-09-20
 * 版    本：1.0.0
 * 功能说明：文章控制器。
 *
 **/

namespace Qwadmin\Controller;

use Vendor\Tree;
use Think\Model;

class NovelController extends ComController
{
	
	public function add()
	{
		
		//获得小说的基本分类
		$class_list = D("novel_class")->where("is_other = 0")->select();
		$this->assign("class_list",$class_list);
		//获得小说的其他分类
		$other_class_list = D("novel_class")->where("is_other = 1")->select();
		$this->assign("other_class_list",$other_class_list);
		
		$this->display('form');
	}
	
	public function index($sid = 0, $p = 1)
	{
		
		//获得小说的基本分类
		$class_list = D("novel_class")->where("is_other = 0")->select();
		
		$this->assign("class_list",$class_list);
		//获得小说的其他分类
		$other_class_list = D("novel_class")->where("is_other = 1")->select();
		
		$this->assign("other_class_list",$other_class_list);
		
		//查询条件
		if(!empty($_GET["name"])) {
			$map["name"] = array("like","%".$_GET["name"]."%");
			$this->assign("search_name",$_GET["name"]);
		}
		if(!empty($_GET["class_id"])){
			$map["class_id"] = $_GET["class_id"];
			$this->assign("search_class_id",$_GET["class_id"]);
		}
		if(!empty($_GET["other_class_id"])){
			$map["other_class_id"] = $_GET["other_class_id"];
			$this->assign("search_other_class_id",$_GET["other_class_id"]);
		}
		if(!empty($_GET["from"])){
			$map["from"] = $_GET["from"];
			$this->assign("from",$_GET["from"]);
		}
		if($_GET["is_end"] != -1 && $_GET["is_end"] != ""){
			$map["is_end"] = $_GET["is_end"];
			$this->assign("is_end",$_GET["is_end"]);
		}
		
		if($_GET["is_shelf"] != -1 && $_GET["is_shelf"] != ""){
			$map["is_shelf"] = $_GET["is_shelf"];
			$this->assign("is_shelf",$_GET["is_shelf"]);
		}
		
		$order = "order_set asc,id desc";
		if(!empty($_GET["order_set"]))
			$order= "order_set asc";
		elseif(!empty($_GET["flow_set"]))
			$order= "flow_set asc";
		$p = $_GET["p"] ? $_GET["p"] : 1;		
		$novel = M('Novel');
		$pagesize = 20;#每页数量
		$offset = $pagesize * ($p - 1);//计算记录偏移量		
		$count = $novel->where($map)->count();
		//接收值 
		
		$list = $novel->where($map)->order($order)->limit($offset . ',' . $pagesize)->select();
		
		foreach($list as $x){
			$dir="<img src= ".$x['img']." /> ";
			
		}
		
		$page = new \Think\Page($count, $pagesize);
		$page = $page->show();
		$this->assign('list', $list);
		$this->assign('page', $page);
		$this->display();
		
	}

	public function desc(){
	//$data = I('post.json_data['number']');
	$arr = json_decode($_POST['number'],true);
	print_r($arr);
	if($arr==1){
		$list = $novel->where($map)->order("order_set desc,id desc")->limit($offset . ',' . $pagesize)->select();
	}else{
		$list = $novel->where($map)->order("order_set asc,id desc")->limit($offset . ',' . $pagesize)->select();
	}
	}
	public function del()
	{
		
		$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : false;
		if ($id) {
			if (is_array($id)) {
				$id = implode(',', $id);
				$map['id'] = array('in', $id);
			} else {
				$map = 'id=' . $id;
			}
			if (M('novel')->where($map)->delete()) {
				addlog('删除文章，AID：' . $id);
				$this->success('恭喜，文章删除成功！');
			} else {
				$this->error('参数错误！');
			}
		} else {
			$this->error('参数错误！');
		}
		
	}
	
	public function edit($id)
	{		
		$id = intval($id);
		
		$novel = M('novel')->where('id=' . $id)->find();
		
		if ($novel) {			
			$this->assign('novel', $novel);
			//获得小说的基本分类
			$class_list = D("novel_class")->where("is_other = 0")->select();	
			$this->assign("class_list",$class_list);
			//获得小说的其他分类
			$other_class_list = D("novel_class")->where("is_other = 1")->select();
			$this->assign("other_class_list",$other_class_list);
			
		} else {
			$this->error('参数错误！');
		}
		$this->display('form');
		
	}
	public function update($novel_id= 0)
	{		
		
		$novel_id= intval($novel_id);
	
		$data['class_id'] = isset($_POST['class_id']) ? intval($_POST['class_id']) : 0;
		$data['class_name'] = isset($_POST['class_name']) ? $_POST['class_name'] : "";
		$data['other_class_id'] = isset($_POST['other_class_id']) ? $_POST['other_class_id'] : 0;
		$data['other_class_name'] = isset($_POST['other_class_name']) ? $_POST['other_class_name'] : "";
		$data['name'] = isset($_POST['title']) ? $_POST['title'] : '';
		$data['description'] = isset($_POST['description']) ? $_POST['description'] : '';
		$data['img'] = I('post.thumbnail', '', 'strip_tags');
		$data['mini_img'] = I('post.thumbnail', '', 'strip_tags');
		$data['order_set'] = isset($_POST['order_set']) ? $_POST['order_set'] : '10000';
		$data['flow_set'] = isset($_POST['flow_set']) ? $_POST['flow_set'] : '10000';
		$data['publish_time'] = date("Y-m-d H:i:s");
		$data['is_flag'] = I('post.is_flag');
		$data['is_shelf']=I('is_shelf');
		$data['is_end']=I('is_end');
		//print_r($data);die;
		if (!$data['name'] or !$data['class_id'] or !$data['other_class_id'] or !$data['img']) {
			$this->error('警告！小说分类、特别分类、标题及小说封面为必填项目。');
		}
		if ($novel_id) {
			M('novel')->data($data)->where('id=' . $novel_id)->save();
			addlog('编辑小说基本信息，novel_id：' . $novel_id);
			$this->success('恭喜！信息编辑成功！', "index.html");
		} else {
			$aid = M('novel')->data($data)->add();
			if ($aid) {
				addlog('新增小说，novel_id：' . $novel_id);
				$this->success('恭喜！小说新增成功！', "index.html");
			} else {
				$this->error('抱歉，未知错误！');
			}
				
		}
	}
	
	/**
	 * 文章列表 
	 * */
	public function chapter($p = 1){
		if(isset($_GET["id"])){
			$id = $_GET["id"];//这个是文章id
			$novel = M('novel')->where(array('id'=>$id))->find();
	
			$this->assign('novel',$novel);
			$this->assign("novel_id",$id);
			$p = intval($p) > 0 ? $p : 1;
			$chapter= M('novel_chapter');
			$pagesize = 15;#每页数量
			$offset = $pagesize * ($p - 1);//计算记录偏移量
			$map["novel_id"] = $id;
			$count = $chapter->where($map)->count();
			header("Content-type:text/html;charset=utf-8");
			$list = $chapter->where($map)->order("order_set asc")->limit($offset . ',' . $pagesize)->select();
			$page = new \Think\Page($count, $pagesize);
			$page = $page->show();
			$this->assign('list', $list);
			$this->assign('page', $page);
			$this->display();
		}
		else{
			$this->error('请选择一篇小说',"index.html");
		}
	}
	/**
	 * 添加章节
	 * */
	public function addchapter(){
		$id = $_GET["novel_id"];
		$novel = D("novel")->where("id=".$id)->find();
		$this->assign("novel_id",$novel["id"]);
		$this->assign("novel_mini_img",$novel["mini_img"]);
		$this->assign("novel_name",$novel["name"]);
		
		$this->display('chapterform');
	}

	/**
	 * 编辑章节
	 * */
	public function editchapter($id){
		$id = intval($id);
		$chapter = M('novel_chapter')->where('id='.$id)->find();
		if ($chapter) {
			
			$this->assign('chapter', $chapter);
			$this->assign('novel_id', $chapter["novel_id"]);
			$this->assign('novel_name', $chapter["novel_name"]);
			$this->assign('novel_mini_img', $chapter["novel_mini_img"]);
			$this->assign('chapter_id', $chapter["id"]);
		} else {
			$this->error('参数错误！');
		}
		$this->display('chapterform');
	}
	
	/**
	 * 更新章节
	 * */
	public function updatechapter($chapter_id){
		$chapter_id= intval($chapter_id);
		$data['chapter'] = isset($_POST['chapter']) ? $_POST['chapter'] : '';
		$data['title'] = isset($_POST['title']) ? $_POST['title'] : '';
		$data['content'] = isset($_POST['content']) ? $_POST['content'] : '';
		$data['novel_id'] = isset($_POST['novel_id']) ? intval($_POST['novel_id']) : 0;
		$data['novel_name'] = isset($_POST['novel_name']) ? $_POST['novel_name'] : '';
		$data['novel_mini_img'] = isset($_POST['novel_mini_img']) ? $_POST['novel_mini_img'] : '';
		$data['order_set'] = isset($_POST['order_set']) ? $_POST['order_set'] : 1000;
		$data['add_time'] = date("Y-m-d H:i:s");
		if (!$data['chapter'] or !$data['title'] or !$data['content'] or !$data['novel_id']) {
			$this->error('警告！章节，标题、内容、关联小说ID必填项目。');
		}
		if ($chapter_id) {
			M('novel_chapter')->data($data)->where('id=' . $chapter_id)->save();
			addlog('编辑小说基本信息，novel_id：' . $novel_id);
			$this->success('恭喜！章节编辑成功！', "chapter/id/".$data['novel_id'].".html");
		} else {
			$aid = M('novel_chapter')->data($data)->add();
			if ($aid) {
				addlog('新增小说，novel_id：' . $novel_id);
				$this->success('恭喜！章节新增成功！', "chapter/id/".$data['novel_id'].".html");
			} else {
				$this->error('抱歉，未知错误！');
			}
			
		}
	}
	
	/**
	 * 删除章节
	 * */
	public function chapterdel(){
		$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : false;
		if ($id) {
			if (is_array($id)) {
				$id = implode(',', $id);
				$map['id'] = array('in', $id);
			} else {
				$map = 'id=' . $id;
			}
			if (M('novel_chapter')->where($map)->delete()) {
				addlog('删除文章，AID：' . $id);
				$this->success('恭喜，文章删除成功！');
			} else {
				$this->error('参数错误！');
			}
		} else {
			$this->error('参数错误！');
		}
	}
	
	/**
	 * 小说更新
	 * 只支持最爽小说平台
	 * */
	public function updatenovel(){
		set_time_limit(0);
		$start_id = $_GET["start_id"] ? $_GET["start_id"] : 0;
		$nmap["is_end"] = 0;
		$nmap["from"] = "最爽小说";
		$update_novel_list = D("novel")->where($nmap)->order("id asc")->limit("$start_id, 1")->select();
		//print_r($update_novel_list);die;
		
		//COOKIE值
		$cookie = "UM_distinctid=15c48573c78204-0470d285b-6a320e21-1fa400-15c48573c79a98; PHPSESSID=260a7e0f704511d86f4bf77b84be168d; uid=21433576; openid=ow7-lwD5Stboe4pp2nW4DD4A3cyw; uname=1f19be8cede01df4936b3fb3249be4; checkstr=e1745060a715236a1bb7f6fae365ab52; sid=260a7e0f704511d86f4bf77b84be168d; sex=2; CNZZDATA1261580920=750510054-1495858397-https%253A%252F%252Fopen.weixin.qq.com%252F%7C1496216642; channel_id=1; promotions_id=1";
		
		if(empty($update_novel_list)){
			die("the end");
		}
		
		foreach($update_novel_list as $key=>$item) {		
			$novel_id = $update_novel_list[0]["id"] ? $update_novel_list[0]["id"] : 3;
			//echo $novel_id;die;
			
			//获得小说的信息
			$novel_info = D("novel")->where("id = $novel_id")->find();
			
			//获得小说的最新章节
			$last_chapter = D("novel_chapter")->where("novel_id = $novel_id")->order("order_set desc")->find();
			//echo D("novel_chapter")->getLastSql();
			
			//echo $last_chapter["chapter"]." ".$novel_info["web_from"]."<br>";
			//模拟微信浏览器访问页面
			$header = fakeIp();
			//$cookie = "PHPSESSID=b131b3e837f61475bf37f85641e0ac6d; uid=21433576; openid=ow7-lwD5Stboe4pp2nW4DD4A3cyw; uname=%E6%AC%A7%E4%BA%9A%E4%BA%91%E4%BA%A4%E6%98%93%E5%AE%A2%E6%9C%8D-%E5%B0%8F%E4%B9%90; checkstr=e1745060a715236a1bb7f6fae365ab52; sid=b131b3e837f61475bf37f85641e0ac6d; sex=2; UM_distinctid=15c497afcb780-0df9e9464-6a320e21-1fa400-15c497afcb93a3; CNZZDATA1261580920=686923403-1495880007-https%253A%252F%252Fopen.weixin.qq.com%252F%7C1495878368; channel_id=1; promotions_id=1";
			$content = getUrlContent($novel_info["web_from"], $header, $cookie, 1);
			
			//echo $content;die;
			//获得小说的最新章节
			$new_start = strpos($content, '<div class="new_mulu line-b">') + strlen('<div class="new_mulu line-b">');
			$new_start = strpos($content, '<p>', $new_start) + strlen('<p>');
			$new_start = strpos($content, '<p>', $new_start) + strlen('<p>');
			$new_end = strpos($content, "</p>", $new_start);
			$new_str = substr($content, $new_start, $new_end - $new_start);
			$new_str = substr($new_str, 0, strpos($new_str, " "));
			$new_order_set = mb_substr($new_str, (mb_strpos($new_str, "第", null, "UTF-8") + mb_strlen("第", "UTF-8")), mb_strpos($new_str, "章", null, "UTF-8") - (mb_strpos($new_str, "第", null, "UTF-8") + mb_strlen("第", "UTF-8")), "UTF-8");
			//echo $new_order_set;die;
			
			
			//判断最新章节是否和我带数据相同
			if($new_order_set <= $last_chapter["order_set"]){
				echo "《".$item["name"]."》"."没有更新章节";
				continue;
			}
			else{ //不相同我们就更新他
				//获取最后一章节
				//计算它是第几页
				$order_set = $last_chapter["order_set"];
				$page = intval($order_set / 20) + 1;
				//echo $page;die;
				//计算它的总也书
				
				//获得第一个目录的链接地址
				$menu_start = strpos($content, '<div class="pub-btn mt-10 pb-0">') + strlen('<div class="pub-btn mt-10 pb-0">');
				$menu_start = strpos($content, '<a href="', $menu_start) + strlen('<a href="');
				$menu_end = strpos($content, '"', $menu_start);
				$menu_url = substr($content, $menu_start, $menu_end - $menu_start);
				$url_list[$page] = "https://zsxs.shu22.cn".$menu_url."&order_by=order%20asc&page=".$page;
				
				//获得最后一页
				$last_page = intval($new_order_set / 20) + 1; 
				
				for($i = $page + 1; $i <= $last_page; $i++){
					$url_list[$i] = "https://zsxs.shu22.cn".$menu_url."&order_by=order%20asc&page=".$i;
				}
				//print_r($url_list);die;
				
				foreach($url_list as $ukey=>$uitem) {			
					//echo $menu_start." ".$menu_end."<br>";
					//echo $menu_url;
					//直接打开列表页面，同时跳转到我们数据对应的那一页。
					$header = fakeIp();
					
					$cha_list = getUrlContent($uitem, $header, $cookie, 1);
					//echo $cha_list;
					$list_start = strpos($cha_list, '<div class="mulu-list container">') + strlen('<div class="mulu-list container">');
					$list_start = strpos($cha_list, '<ul>', $list_start) + strlen('<ul>');
					$list_end = strpos($cha_list, '</ul>', $list_start);
					$list = substr($cha_list, $list_start, $list_end - $list_start);
					//echo $list."<br>";
					
					$list = explode('<a href="', $list);
					//print_r($list);die;
					$index = 0;
					$data = array();
					foreach($list as $key=>$item){
						//echo $item."<br>";
						//获得URL
						$url = substr($item, 0, strpos($item, '"'));
						//获得章节
						$chapter = substr($item, strpos($item, '<span>')+strlen('<span>'),  strpos($item, '</span>') - strpos($item, '<span>')+strlen('<span>'));
						$chapter = mb_substr($chapter, 0,  (mb_strpos($chapter, "章", null, "UTF-8") + mb_strlen("章", "UTF-8")), "UTF-8");
						//获得order_set
						$item_order_set =  mb_substr($chapter, (mb_strpos($chapter, "第", null, "UTF-8") + mb_strlen("第", "UTF-8")), mb_strpos($chapter, "章", null, "UTF-8") - (mb_strpos($chapter, "第", null, "UTF-8") + mb_strlen("第", "UTF-8")), "UTF-8");				
						if(strlen($url) > 0 && strlen($chapter) > 0) {						
							if($item_order_set > $order_set) {
								//echo $chapter." ".$url."<br>";die;
								$header = fakeIp();
								//$cookie = "PHPSESSID=b131b3e837f61475bf37f85641e0ac6d; uid=21433576; openid=ow7-lwD5Stboe4pp2nW4DD4A3cyw; uname=%E6%AC%A7%E4%BA%9A%E4%BA%91%E4%BA%A4%E6%98%93%E5%AE%A2%E6%9C%8D-%E5%B0%8F%E4%B9%90; checkstr=e1745060a715236a1bb7f6fae365ab52; sid=b131b3e837f61475bf37f85641e0ac6d; sex=2; UM_distinctid=15c497afcb780-0df9e9464-6a320e21-1fa400-15c497afcb93a3; CNZZDATA1261580920=686923403-1495880007-https%253A%252F%252Fopen.weixin.qq.com%252F%7C1495878368; channel_id=1; promotions_id=1";
								//echo $url;die;
								//$url = "https://zsxs.shu22.cn/book/content/?bookid=2023870&chapterid=5614267";
								//$zs_url = "https://zsxs.shu22.cn/book/content/?bookid=2023870&chapterid=5571491";
								$url = substr($url, 0, strpos($url, "?"));
								$ids = substr($url, strpos($url, "content/") + strlen("content/"), strlen($url) - strpos($url, "content/") + strlen("content/"));
								$bookid = substr($ids, 0, strpos($ids, "/"));
								$chapterid = substr($ids, strpos($ids, "/") + 1, strlen($ids) - strpos($ids, "/") + 1);
								//echo $bookid." ".$chapterid;die;
								$zs_url = "https://zsxs.shu22.cn/book/content/?bookid=$bookid&chapterid=$chapterid";
								//echo $zs_url;die;
								//$bookid = 
								$chapter_content = getUrlContent($zs_url,$header,$cookie,1);
								//echo $c_url."<br>";
								
								//判断抓取是否有效
								$is_error = strpos($chapter_content, "https://res.wx.qq.com/open/libs/weui/0.4.1/weui.css");
								if(!empty($is_error)) {
									print_r($rs);
									echo $chapter_content;
									die("我呢个去你的cookie有问题,问题出现在".$novel_info["novel_name"]."下的第".$ukey."页的第".($key)."条");
								}
								
								//echo $chapter_content;die;
								
								//判断是否已经没有钱了
								//如果没有就不添加数据
// 								$no_money = strpos($chapter_content, "<title>最爽小说-充值</title>");
// 								if(!empty($no_money))
// 									die("兄弟你没钱了")
								if(strlen($chapter_content) == 0)
									die("兄弟你没钱了");
									
								//echo "1111".$chapter_content;die;
								
								//获取标题 title
								$title_start = strpos($chapter_content, '<div class="yd-left">') + strlen('<div class="yd-left">');
								$title_start= strpos($chapter_content, '<h3>', $title_start) + strlen('<h3>');
								$title_start= strpos($chapter_content, '章', $title_start) + strlen('章');
								$title_end = strpos($chapter_content, '</h3>', $title_start);
								$title = trim(substr($chapter_content, $title_start, $title_end- $title_start));
								
								//获取第几章节(有中文)chapeter
								$cha_start = strpos($chapter_content, '<div class="yd-left">') + strlen('<div class="yd-left">');
								$cha_start = strpos($chapter_content, '<h3>', $cha_start) + strlen('<h3>');
								$cha_start = strpos($chapter_content, '第', $cha_start);
								$cha_end = strpos($chapter_content, '章', $cha_start) + strlen('章');
								$chapter_value= substr($chapter_content, $cha_start, $cha_end- $cha_start);
								//echo $title."<br>".$chapter_value;die;
								
								//判断是否章节重复
								$map["novel_id"] = $novel_info["id"];
								$map["chapter"] = $chapter_value;
								$count = D("novel_chapter")->where($map)->count();
								//echo $count;die;
								if($count > 0)
									continue;
									
								//标题赋值
								$data[$index]["title"] = $title ? $title : " ";
								$data[$index]["chapter"] = $chapter_value;
								
								//获取order_set
								$order_start = strpos($data[i]["chapter"], '第') + strlen('第');
								$order_end = strpos($data[i]["chapter"], '章', $order_start);
								$data[$index]["order_set"] = substr($data[$index]["chapter"], $order_start, $order_end- $order_start);
								
								//获取章节内容
								$c_pos_start = strpos($chapter_content, '<div class="yd-text" style="font-size:16px;">') + strlen('<div class="yd-text" style="font-size:16px;">');
								$c_pos_end = strpos($chapter_content, '</div>', $c_pos_start);
								$data[$index]["content"]= substr($chapter_content, $c_pos_start, $c_pos_end - $c_pos_start);
								
								//设置数据
								$data[$index]["novel_name"] = $novel_info["name"];
								$data[$index]["novel_id"] = $novel_info["id"];
								$data[$index]["novel_mini_img"] = $novel_info["mini_img"];
								$data[$index]["add_time"] = date("Y-m-d H:i:s");
								
								//print_r($data);die;
								
								//索引自动增长
								$index++;
							}						
						}
					}
					//print_r($data);die;
					if(!empty($data))
						D("novel_chapter")->addAll($data);
				}
			}
		}
		
// 		if($start_id > 0) {
 			echo "the end";
 			die;
// 		}
		
		$next_start_id = $start_id + 1;
		//$this->redirect("/Qwadmin/Novel/updatenovel?start_id=$next_start_id");
		$this->success("跳转到下一章节","/Qwadmin/Novel/updatenovel?start_id=$next_start_id",1);
		//echo "the end";
		//echo $content;
	}
	
	/**
	 * 采集小说目录
	 * */
	public function collectList(){
		$id = $_GET["id"];
		$this->assign("novel_id",$id);
		
		$p = intval($p) > 0 ? $p : 1;
		$chapter= M('novel_get');
		$pagesize = 20;#每页数量
		$offset = $pagesize * ($p - 1);//计算记录偏移量
		
		$map["novel_id"] = $id;
		$count = $chapter->where($map)->count();
		$list = $chapter->where($map)->order("id asc")->limit($offset . ',' . $pagesize)->select();
		
		$page = new \Think\Page($count, $pagesize);
		$page = $page->show();
		$this->assign('list', $list);
		$this->assign('page', $page);
		
		$this->display();
	}
	
	/**
	 * 新增采集小说目录
	 * */
	public function collectAdd(){
		
	}
	
	/**
	 * 采集小说名称相关数据，不包括章节
	 * 只采集小说的名称和介绍
	 * 最爽小说针对http://zsxs.shu22.cn
	 * */
	public function getNovelInfo(){
		$rs = D("novel_get")->select();
		//print_r($rs);die;
		
		foreach ($rs as $key=>$item){
			$data = array();
			//获得小说图片版面内容
			$header = fakeIp();
			$novel = getUrlContent($item["web_from"],$header);
			
			//小说名称
			$data["name"] = $item["novel_name"];
			
			//判断小说是否已经存在了
			$novel_map["name"] = $item["novel_name"];
			//echo '<meta charset="UTF-8">';
			$novel_count = D("novel")->where($novel_map)->count();
			//echo D()->getLastSql();die;
			if($novel_count > 0){
				D("novel_get")->where("id = ".$item["id"])->delete();
				continue;
			}
			
			//来自地址
			$data["web_from"] = $item["web_from"];
			$data["from"] = $item["from"];
			
			//分类
			$data["class_id"] = $item["class_id"];
			$data["class_name"] = $item["class_name"];
			
			//男生，女生
			$data["other_class_id"] = $item["other_class_id"];
			$data["other_class_name"] = $item["other_class_name"];
			
			//小说图片
			$img_start = strpos($novel, '<div class="xq-left">') + strlen('<div class="xq-left">');
			$img_start = strpos($novel, '<img src="', $img_start) + strlen('<img src="');
			$img_end = strpos($novel, '"', $img_start);
			$img_url = substr($novel, $img_start, $img_end - $img_start);
			//echo $img_url;
			$img = getWebImg($img_url);
			$data["img"] = $img["save_path"].$img["file_name"];
			$data["mini_img"] = $img["save_path"].$img["file_name"];
			
			//总字数
			$font_start = strpos($novel, '字数：') + strlen('字数：');
			$font_end = strpos($novel, '</p>',$font_start);
			$word_count = substr($novel, $font_start, $font_end- $font_start);
			$data["word_count"] = $word_count;
			
			//作者
			$w_start = strpos($novel, '作者：') + strlen('作者：');
			$w_end = strpos($novel, '</p>',$w_start);
			$writer= substr($novel, $w_start, $w_end- $w_start);
			$data["writer"] = $writer;
			
			//是否完结
			$end_start = strpos($novel, '连载中');
			if(!empty($end_start))				
				$data["is_end"] = 0;
			else 
				$data["is_end"] = 1;
			//echo '<meta charset="UTF-8">';
			
			D("novel_get")->where("id = ".$item["id"])->delete();
			D("novel")->add($data);
// 			echo '<meta charset="UTF-8">';
// 			print_r($data);
		}
		
		//跳转抓取文章内容
		//获取没有爬取的所有文章
		$r_count = D("novel")->where("is_over = 0")->count();
		$url = "http://".$_SERVER["HTTP_HOST"]."/Qwadmin/Novel/newGetChapterInfo?reptile_start=0&page_start=0&page_end=".$r_count."&table_name=novel_chapter_0";
		header("Location: $url");
	}
	
	/**
	 * 采集小说章节
	 * 最爽小说
	 * */
	function newGetChapterInfo(){
		set_time_limit(0);
		$reptile_start = $_GET["reptile_start"] ? $_GET["reptile_start"] : 0;
		$rs = D("novel")->where("`name` = '谁动了我的老婆'")->limit("0,1")->order("id asc")->select();
		
		$page_start = $_GET["page_start"] ? $_GET["page_start"] : 0;
		$page_end = $_GET["page_end"] ? $_GET["page_end"] : 0;
		$table_name= $_GET["table_name"] ? $_GET["table_name"] : '';
		echo '<meta charset="UTF-8">';
		//print_r($rs);die;
		
		
		//判断必要条件
		if((empty($page_start) && $page_start != 0) || empty($page_end)  || empty($table_name))
			die("not limit please set page_start and page_end and table_name");
		
		//判断是否达到限制
		if($reptile_start >= $page_end) {
			die("the end");
		}

		//小说展示页面地址
		$url = $rs[0]["web_from"];
		//echo $url;
		//获得小说展示页面
		$header = fakeIp();
		$cookie = "PHPSESSID=b131b3e837f61475bf37f85641e0ac6d; uid=21433576; openid=ow7-lwD5Stboe4pp2nW4DD4A3cyw; uname=%E6%AC%A7%E4%BA%9A%E4%BA%91%E4%BA%A4%E6%98%93%E5%AE%A2%E6%9C%8D-%E5%B0%8F%E4%B9%90; checkstr=e1745060a715236a1bb7f6fae365ab52; sid=b131b3e837f61475bf37f85641e0ac6d; sex=2; UM_distinctid=15c497afcb780-0df9e9464-6a320e21-1fa400-15c497afcb93a3; CNZZDATA1261580920=686923403-1495880007-https%253A%252F%252Fopen.weixin.qq.com%252F%7C1495878368; channel_id=1; promotions_id=1";
		$content = getUrlContent($url, $header, $cookie, 1);
		
		//echo($content);die;
		
		//获得小说的最新章节
		//同时计算小说有多少也
		//echo $content;
		$best_start = strpos($content, '<div class="new_mulu line-b">') + strlen('<div class="new_mulu line-b">');
		$best_start = strpos($content, '第', $best_start) + strlen("第");
		$best_end = strpos($content, "章", $best_start);
		$chapter_count = substr($content, $best_start, ($best_end - $best_start));
		//echo $chapter_count;die;
		
		//获得小说多少页
		if(intval($chapter_count) % 20 == 0)
			$novel_page = intval($chapter_count) / 20;
		else 
			$novel_page = intval(intval($chapter_count) / 20 + 1);
		
		//echo $novel_page;die;
		
		//echo $novel_page;
		//获得小说的原始网站id
		$web_novel_id = substr($url, (strpos($url, "bookid=") + strlen("bookid=")),  strlen($url) - (strpos($url, "bookid=") + strlen("bookid=")));
		//echo $web_novel_id;die;
		
		//循环爬取小说		
		for($i = 15; $i <= $novel_page; $i++){
// 			if($i > 1)
// 				continue;
			//每次循环的地址
			$chapter_url = "http://zsxs.shu22.cn/book/chapter?bookid=".$web_novel_id."&page=$i";
			//伪造IP
			$header = fakeIp();
			$chapter_list = getUrlContent($chapter_url,$header,$cookie,1);			
			
			//获得小说位置
			$list_pos_begin = strpos($chapter_list, '<div class="mulu-list container">') + strlen('<div class="mulu-list container">');
			$list_pos_begin = strpos($chapter_list, '<ul>', $list_pos_begin) + strlen('<ul>');
			$list_pos_end = strpos($chapter_list, '</ul>', $list_pos_begin);
			
			$chapter_list = substr($chapter_list, $list_pos_begin, $list_pos_end - $list_pos_begin);
			$chapter_list = explode('<a href="', $chapter_list);
			//print_r($chapter_list);die;
			$data = array();
			$index = 0;
			
			
			foreach($chapter_list as $key=>$item){						
				$c_url = substr($item, 0, strpos($item, '"'));
				//echo $c_url."<br>";
				if(strlen($c_url) > 0){					
					//echo $c_url."<br>";
					$c_url = substr($item, 0, strpos($item, '"'));
					$is_token = strpos($c_url, "token");
					if(empty($is_token)){						
						$c_url = "http://zsxs.shu22.cn".$item;
					}
  				//echo $c_url."<br>";die;
// 					continue;
					//伪造IP
					//获得整夜内容
					//print_r($_SERVER);die;
					//echo $c_url;die;
					$header = fakeIp();
					$cookie = "PHPSESSID=b131b3e837f61475bf37f85641e0ac6d; uid=21433576; openid=ow7-lwD5Stboe4pp2nW4DD4A3cyw; uname=%E6%AC%A7%E4%BA%9A%E4%BA%91%E4%BA%A4%E6%98%93%E5%AE%A2%E6%9C%8D-%E5%B0%8F%E4%B9%90; checkstr=e1745060a715236a1bb7f6fae365ab52; sid=b131b3e837f61475bf37f85641e0ac6d; sex=2; UM_distinctid=15c497afcb780-0df9e9464-6a320e21-1fa400-15c497afcb93a3; CNZZDATA1261580920=686923403-1495880007-https%253A%252F%252Fopen.weixin.qq.com%252F%7C1495878368; channel_id=1; promotions_id=1";
					$chapter_content = getUrlContent($c_url,$header,$cookie,1);	
					//echo $c_url."<br>";
					//echo $chapter_content;die;
					
					//判断抓取是否有效
					$is_error = strpos($chapter_content, "https://res.wx.qq.com/open/libs/weui/0.4.1/weui.css");
					if(!empty($is_error)) {
						print_r($rs);
						echo $chapter_content;
						die("我呢个去你的cookie有问题,问题出现在".$rs[0]["novel_name"]."下的第".$i."页的第".$key."条");
					}
					
					//判断是否已经没有钱了
					//如果没有就不添加数据
					$no_money = strpos($chapter_content, "<title>最爽小说-充值</title>");
					if(!empty($no_money))
						die("兄弟你没钱了");
					
					//echo $chapter_content;die;	
					
					//获取标题 title
					$title_start = strpos($chapter_content, '<div class="yd-left">') + strlen('<div class="yd-left">');
					$title_start= strpos($chapter_content, '<h3>', $title_start) + strlen('<h3>');
					$title_start= strpos($chapter_content, '章', $title_start) + strlen('章');
					$title_end = strpos($chapter_content, '</h3>', $title_start);
					$title = trim(substr($chapter_content, $title_start, $title_end- $title_start));
					
					//获取第几章节(有中文)chapeter
					$cha_start = strpos($chapter_content, '<div class="yd-left">') + strlen('<div class="yd-left">');
					$cha_start = strpos($chapter_content, '<h3>', $cha_start) + strlen('<h3>');
					$cha_start = strpos($chapter_content, '第', $cha_start);
					$cha_end = strpos($chapter_content, '章', $cha_start) + strlen('章');
					$chapter_value= substr($chapter_content, $cha_start, $cha_end- $cha_start);
					//echo $title."<br>".$chapter_value;die;
					
					//判断是否章节重复
					$map["novel_id"] = $rs[0]["id"];
					$map["chapter"] = $chapter_value;
					$count = D("novel_chapter")->where($map)->count();
					//echo $count;die;
					if($count > 0)
						continue;
					
					//标题赋值
					$data[$index]["title"] = $title ? $title : " ";
					$data[$index]["chapter"] = $chapter_value;
					
					//获取order_set
					$order_start = strpos($data[i]["chapter"], '第') + strlen('第');
					$order_end = strpos($data[i]["chapter"], '章', $order_start);
					$data[$index]["order_set"] = substr($data[$index]["chapter"], $order_start, $order_end- $order_start);
					
					//获取章节内容
					$c_pos_start = strpos($chapter_content, '<div class="yd-text" style="font-size:16px;">') + strlen('<div class="yd-text" style="font-size:16px;">');
					$c_pos_end = strpos($chapter_content, '</div>', $c_pos_start);					
					$data[$index]["content"]= substr($chapter_content, $c_pos_start, $c_pos_end - $c_pos_start);
					
					//设置数据
					$data[$index]["novel_name"] = $rs[0]["name"];
					$data[$index]["novel_id"] = $rs[0]["id"];
					$data[$index]["novel_mini_img"] = $rs[0]["mini_img"];
					$data[$index]["add_time"] = date("Y-m-d H:i:s");
					
					//索引自动增长
					$index++;
					
					//$new_data = $data[$key];
					//D("novel_chapter")->addAll($data);die("end");
					//print_r($data);die;
					//echo $chapter_content;
				}
			}
			//print_r($data);
			//添加章节数据20条
			if(!empty($data))
				D("novel_chapter_add")->addAll($data);			
		}
		
		//修改当前小说为已经爬取状态
// 		$map["id"]=$rs[0]["id"];
// 		$novel_data["is_over"] = 1; 
// 		D("novel")->where($map)->save($novel_data);

		//跳转下一本小说
		$this->success(
				'continue next chapter',
				"/Qwadmin/Novel/newGetChapterInfo?reptile_start=".($reptile_start+ 1)."&page_start=".$page_start."&page_end=".$page_end."&table_name=".$table_name,
				1);
			
	}
	
	/**
	 * 获得同城群聊的小说列表
	 * 只获得小说的相关信息，不包括小说的章节
	 * */
	public function getnovel2(){
		set_time_limit(0);
		
		//分类URL
			
		$class_list = array(				
			array("url"=>"https://c251.818tu.com/novels/api_list_by_category/11?","name"=>"都市","class_id"=>"24","id"=>11),
			array("url"=>"https://c251.818tu.com/novels/api_list_by_category/13?","name"=>"历史","class_id"=>"27","id"=>13),
			array("url"=>"https://c251.818tu.com/novels/api_list_by_category/15?","name"=>"总裁豪门","class_id"=>"28","id"=>15),
			array("url"=>"https://c251.818tu.com/novels/api_list_by_category/16?","name"=>"官场红门","class_id"=>"29","id"=>16),
			array("url"=>"https://c251.818tu.com/novels/api_list_by_category/17?","name"=>"恐怖惊悚","class_id"=>"30","id"=>17),
			array("url"=>"https://c251.818tu.com/novels/api_list_by_category/18?","name"=>"校园青春","class_id"=>"31","id"=>18),
			array("url"=>"https://c251.818tu.com/novels/api_list_by_category/20?","name"=>"玄幻仙侠","class_id"=>"32","id"=>20),
		);
		
		foreach($class_list as $key=>$item){			
			$start = 0;
			while(1){
				$url = $item["url"]."start=$start";	
				$header = fakeIp();
				$url_list = getUrlContent($url, $header, null, 1);
				$url_list = json_decode($url_list, true);
				echo "<meta charset='utf-8'>";
				//print_r($url_list);die;
				foreach($url_list as $ukey=>$uitem){
	//				$img = getWebImg("https://ommdq027l.qnssl.com/novels/1495427096821.jpg", 1);die;
	// 				echo substr($uitem["avatar"], 0, strpos($uitem["avatar"], "?"));
	//				print_r($uitem);die;
					
					$data["name"] = $uitem["title"];
					$data["description"] = $uitem["summary"];
					//获取图片				
					$img = getWebImg($uitem["avatar"] , 1);
					$data["img"] = $img["save_path"].$img["file_name"];
					$data["mini_img"] = $img["save_path"].$img["file_name"];
					//类型
					$data["class_id"] = $item["class_id"];
					$data["class_name"] = $item["name"];
					$data["other_class_id"] = "3";
					$data["other_class_name"] = "男生";
					//来源
					$data["from"] = "同城群聊";
					$data["web_from"] = "https://c251.818tu.com/read/api_get_catalog/".$uitem["id"];
					//$data["from_url"] = "https://c251.818tu.com/read/novel/".$uitem["id];
					//print_r($data);
					//先查询小说是否存在
					$map["name"] = $uitem["title"];
					$count = D("novel")->where($map)->count();
					if($count == 0)
						D("novel")->add($data);
					//die("end");
				}
				
				if(count($url_list) < 20)
					break;
				else 
					$start += 20;
			}
		}
		
		echo "the end";		
	}
	
	/**
	 * 获取同城群聊相关的小说章节 
	 * */
	public function getchapter2(){
		set_time_limit(0);
		$id = $_GET["id"] ? $_GET["id"] :59;
		$map["from"] = "同城群聊";
		$map["id"] = $id;
		//echo $id;
		$novel_list = D("novel")->where($map)->select();
		//echo D("novel")->getLastSql();die;
		//print_r($novel_list);
		echo "<meta charset='utf-8'>";
		foreach($novel_list as $key=>$item){
			$start = 0;
			while(1){
				$header = fakeIp();
				$url = $item["web_from"]."?start=$start";
				
				//$url = "https://c251.818tu.com/read/novel/70?from=";
				$chapter_list = getUrlContent($url,$header,null,1);
				$chapter_list= json_decode($chapter_list, true);
// 				echo $url."<br>";
// 				if(count($chapter_list) < 50)
// 					break;
// 				else
// 					$start += 50;
				
// 				continue;
//				print_r($chapter_list);die;
				
				$add_data = array();
				$add_index = 0;
				foreach($chapter_list as $ckey=>$citem){
					$data["chapter"] = "第".$citem["idx"]."章";
					//过滤同城群聊的小说标题
					$title = $citem["title"];
					//echo $title."<br>";
					//先判断有章
// 					$z_pos = mb_strpos($title,"章",null,"UTF-8") + mb_strlen("章", "UTF-8");
// 					$j_pos = mb_strpos($title,"节",null,"UTF-8") + mb_strlen("节", "UTF-8");;
					//echo $j_pos." ".$z_pos."<br>";die;
					//die;
					if(mb_strpos($title,"章",null,"UTF-8")){
						$pos_start = mb_strpos($title,"章",null,"UTF-8") + mb_strlen("章", "UTF-8");
						$title = mb_substr($title, $pos_start, (strlen($title) - $pos_start), "UTF-8");
						
						//过滤特别字符
						$title = str_replace(" ", "", $title);
						$title = str_replace("、", "", $title);
						$title = str_replace("·", "", $title);
						$title = str_replace(".", "", $title);
						$title = str_replace("（", "", $title);
						$title = str_replace("）", "", $title);
						$title = str_replace("：", "", $title);
					}
					elseif(mb_strpos($title,"节",null,"UTF-8")){
						$pos_start = mb_strpos($title,"节",null,"UTF-8") + mb_strlen("节", "UTF-8");
						$title = mb_substr($title, $pos_start, (strlen($title) - $pos_start), "UTF-8");
						//echo $title." ".strlen($title)." ".$pos_start."<br>";
						
						//过滤特别字符
						$title = str_replace(" ", "", $title);
						$title = str_replace("、", "", $title);
						$title = str_replace("·", "", $title);
						$title = str_replace(".", "", $title);
						$title = str_replace("【", "", $title);
						$title = str_replace("】", "", $title);
						$title = str_replace("：", "", $title);
						
					}
					else{
						//过滤数字
						for($i=0;$i<10;$i++)
							$title = str_replace("$i", "", $title);
						//过滤特别字符
						$title = str_replace(" ", "", $title);
						$title = str_replace("、", "", $title);
						$title = str_replace("·", "", $title);
						$title = str_replace(".", "", $title);
						$title = str_replace("【", "", $title);
						$title = str_replace("】", "", $title);
						$title = str_replace("（", "", $title);
						$title = str_replace("）", "", $title);
						$title = str_replace("：", "", $title);
					}
					$data["title"] = $title;
					//echo $title;die;
					
					$data["chapter"] = "第".$citem["idx"]."章";
					
					$data["order_set"] = $citem["idx"];
					
					$data["add_time"] = date("Y-m-d H:i:s");
					
					//查询重复
					$c_map["order_set"] =  $citem["idx"];
					$c_map["novel_id"] = $item["id"];
					$count = D("novel_chapter")->where($c_map)->count();
					if($count > 0) {
						continue;
					}
					
					$header = fakeIp();
					$c_url = "https://c251.818tu.com/read/index/".$citem["id"]."?from=";
					$cookie = "UM_distinctid=15c48826fac86e-0dd93756d-6a320e21-1fa400-15c48826fad294; __fvmp=1; __trade_type=jsapi; CNZZDATA1260438811=1003124604-1495859966-%7C1495868746; member_token=eb611820526446529e267dca399a0c5f";
					$chapter_content = getUrlContent($c_url,$header,$cookie,1);
					$c_start = strpos($chapter_content, "<section>") + strlen("<section>");
					$c_end = strpos($chapter_content, "</section>", $c_start);
					$chapter_content = substr($chapter_content, $c_start, $c_end - $c_start);		
					
					if(empty($chapter_content) || strlen($chapter_content) <= 0){
						echo "session失效了，id在$id";
						die;
					}
					
					
					$data["content"] = $chapter_content;
					
					//对应小说信息
					$data["novel_name"] = $item["name"];
					$data["novel_id"] = $item["id"];
					$data["novel_mini_img"] = $item["mini_img"];
					
					//print_r($data);die;
								
					$add_data[$add_index] = $data;
					$add_index++;	
					//print_r($data);
				}
				
				if(!empty($add_data)) {
					//print_r($add_data);die;
					D("novel_chapter")->addAll($add_data);
				}
				
				if(count($chapter_list) >= 50)
					$start += 50;
				else 
					break;
			}
			//die;
			break;
		}
		
		//echo "end";
		if(intval($id) == 285) {
			echo "is end";
			die;
		}
		else {
			$next_id = $id + 1;
			$this->redirect("http://www.mytpadmin.com/Qwadmin/Novel/getchapter2?id=$next_id");
		}
	}
	
	/**
	 * 同城群聊数据同步修改
	 * */
	public function tongchengdatachange(){
		set_time_limit(0);
		
		$map["from"] = '同城群聊';
		$rs = $novel = D("novel")->where($map)->select();
		foreach($rs as $key=>$item){
			$new_web_from = $item["web_from"];
			$new_start = strpos($new_web_from, "api_get_catalog/") + strlen("api_get_catalog/");			
			$new_web_from = "https://c251.818tu.com/api/novels/catalog/".substr($new_web_from, $new_start, strlen($new_web_from) - $new_start);
			
			$data["web_from"] = $new_web_from;
			D("novel")->where("id=".$item["id"])->save($data);
		}
		
		echo "end";
	}
	
	/**
	 * 小说更新
	 * 只支持同城群聊平台
	 * */
	public function updatenovel2(){
		set_time_limit(0);
		$map["from"] = '同城群聊';
		$novel_start = $_GET["novel_start"] ? $_GET["novel_start"] : 0;
		$novel_list = D("novel")->where($map)->limit("$novel_start,1")->select();
		//echo D()->getLastSql();die;
		$cookie = $_GET["cookie"] ? $_GET["cookie"] : "UM_distinctid=15c66ac7fbe48e-0acc67398-6a320e21-1fa400-15c66ac7fbf72f; __fvmp=1; __trade_type=jsapi; CNZZDATA1260438811=877790096-1496367568-%7C1496387148; member_token=fdd4954d00e3474eaa887188f03bc12d";
		
		//完成
		if(empty($novel_list))
			die("the end");
		
		foreach($novel_list as $key=>$item){
			//获得这篇小说的最后一章节
			$cmap["novel_id"] = $item["id"];			
			$last_chapter = D("novel_chapter")->where($cmap)->order("id desc")->find();
			//print_r($last_chapter);die;
			//echo $item["web_from"]."?start=".$last_chapter["order_set"]."&fetch_novel=0";
			$start = $last_chapter["order_set"];
			$add_data = array();
			$add_index = 0;
			while(1){
				$url = $item["web_from"]."?start=".$start."&fetch_novel=0";
				//获得最新章节的列表
				$header = fakeIp();				
				$json = getUrlContent($url, $header, $cookie, 1);
				$json = json_decode($json, true);
				//echo count($json["data"]["catalog"]);die;
				foreach($json["data"]["catalog"] as $ckey=>$citem){
// 					$c_url = "https://c251.818tu.com/read/index/".$item["id"]."?from=";
// 					$header = fakeIp();
// 					$content = getUrlContent($c_url, $header, $cookie, 1);
					
					$data["chapter"] = "第".$citem["idx"]."章";
					//过滤同城群聊的小说标题
					$title = $citem["title"];
					//echo $title."<br>";
					//先判断有章
					// 					$z_pos = mb_strpos($title,"章",null,"UTF-8") + mb_strlen("章", "UTF-8");
					// 					$j_pos = mb_strpos($title,"节",null,"UTF-8") + mb_strlen("节", "UTF-8");;
					//echo $j_pos." ".$z_pos."<br>";die;
					//die;
					if(mb_strpos($title,"章",null,"UTF-8")){
						$pos_start = mb_strpos($title,"章",null,"UTF-8") + mb_strlen("章", "UTF-8");
						$title = mb_substr($title, $pos_start, (strlen($title) - $pos_start), "UTF-8");
						
						//过滤特别字符
						$title = str_replace(" ", "", $title);
						$title = str_replace("、", "", $title);
						$title = str_replace("·", "", $title);
						$title = str_replace(".", "", $title);
						$title = str_replace("（", "", $title);
						$title = str_replace("）", "", $title);
						$title = str_replace("：", "", $title);
					}
					elseif(mb_strpos($title,"节",null,"UTF-8")){
						$pos_start = mb_strpos($title,"节",null,"UTF-8") + mb_strlen("节", "UTF-8");
						$title = mb_substr($title, $pos_start, (strlen($title) - $pos_start), "UTF-8");
						//echo $title." ".strlen($title)." ".$pos_start."<br>";
						
						//过滤特别字符
						$title = str_replace(" ", "", $title);
						$title = str_replace("、", "", $title);
						$title = str_replace("·", "", $title);
						$title = str_replace(".", "", $title);
						$title = str_replace("【", "", $title);
						$title = str_replace("】", "", $title);
						$title = str_replace("：", "", $title);
						
					}
					else{
						//过滤数字
						for($i=0;$i<10;$i++)
							$title = str_replace("$i", "", $title);
							//过滤特别字符
							$title = str_replace(" ", "", $title);
							$title = str_replace("、", "", $title);
							$title = str_replace("·", "", $title);
							$title = str_replace(".", "", $title);
							$title = str_replace("【", "", $title);
							$title = str_replace("】", "", $title);
							$title = str_replace("（", "", $title);
							$title = str_replace("）", "", $title);
							$title = str_replace("：", "", $title);
					}
					$data["title"] = $title;
					//echo $title;die;
					
					$data["chapter"] = "第".$citem["idx"]."章";
					
					$data["order_set"] = $citem["idx"];
					
					$data["add_time"] = date("Y-m-d H:i:s");
					
					//查询重复
					$c_map["order_set"] =  $citem["idx"];
					$c_map["novel_id"] = $item["id"];
					$count = D("novel_chapter")->where($c_map)->count();
					if($count > 0) {
						continue;
					}
					
					$header = fakeIp();
					$c_url = "https://c251.818tu.com/read/index/".$citem["id"]."?from=";
					//$cookie = "UM_distinctid=15c48826fac86e-0dd93756d-6a320e21-1fa400-15c48826fad294; __fvmp=1; __trade_type=jsapi; CNZZDATA1260438811=1003124604-1495859966-%7C1495868746; member_token=eb611820526446529e267dca399a0c5f";
					$chapter_content = getUrlContent($c_url,$header,$cookie,1);
					$c_start = strpos($chapter_content, "<section>") + strlen("<section>");
					$c_end = strpos($chapter_content, "</section>", $c_start);
					$chapter_content = substr($chapter_content, $c_start, $c_end - $c_start);
					
					if(empty($chapter_content) || strlen($chapter_content) <= 0){
						echo "session失效了，id在$id";
						die;
					}
					
					
					$data["content"] = $chapter_content;
					
					//对应小说信息
					$data["novel_name"] = $item["name"];
					$data["novel_id"] = $item["id"];
					$data["novel_mini_img"] = $item["mini_img"];
					
					//print_r($data);die;
					
					$add_data[$add_index] = $data;
					$add_index++;	
				}
				
				//print_r($add_data);die;
				
				if(!empty($add_data)) {
					D("novel_chapter")->addAll($add_data);
				}
				
				if(count($json["data"]["catalog"]) < 50)
					break;
				else
					$start += 50;
			}
		}
		
		//跳转
		$this->success("抓取下一篇小说","http://www.mytpadmin.com/Qwadmin/Novel/updatenovel2?novel_start=".($novel_start + 1)."&cookie=".$cookie, 1);
	}
}